1
|
|
|
const {setInputType, removeQuotes} = require('./commonFuncs.js') |
2
|
|
|
|
3
|
|
|
// ===================textFrom================================= |
4
|
|
|
export function textFrom (inpObj, cmdArgs, inclusive) { |
5
|
|
|
// cmdArgs is an array of arguments |
6
|
|
|
// inpObj is an object containing the text object we need to snip contents from |
7
|
|
|
// the format of the call is eg. |
8
|
|
|
// '--textFrom "some text" 2 - would start from the second instance of "some text" |
9
|
|
|
if (setInputType(inpObj, 'plain')) { |
10
|
|
|
let x = findLocation(inpObj, cmdArgs, inclusive ? '--start' : '--from') |
11
|
|
|
if (x.found) { |
12
|
|
|
inpObj.plain = inpObj.plain.substr(x.loc + (inclusive === true ? 0 : x.len)) |
13
|
|
|
} |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
// ===================textTo=================================== |
18
|
|
|
export function textTo (inpObj, cmdArgs, inclusive) { |
19
|
|
|
// cmdArgs is an array of arguments |
20
|
|
|
// inpObj is an object containing the text object we need to snip contents from |
21
|
|
|
// the format of the call is eg. |
22
|
|
|
// '--textTo "some text" 2 - would go up to the second instance of "some text" |
23
|
|
|
if (setInputType(inpObj, 'plain')) { |
24
|
|
|
let x = findLocation(inpObj, cmdArgs, inclusive ? '--finish' : '--to') |
25
|
|
|
if (x.found) { |
26
|
|
|
inpObj.plain = inpObj.plain.substring(0, x.loc + (inclusive === true ? x.len : 0)) |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function findLocation (inpObj, cmdArgs, errString) { |
32
|
|
|
// find the location of the nth occurrence of the text specified in the command arguments |
33
|
|
|
let occ |
34
|
|
|
switch (cmdArgs.length) { |
35
|
|
|
case 1: |
36
|
|
|
occ = 1 // by default we take from the first occurrence of this text |
37
|
|
|
break |
38
|
|
|
case 2: |
39
|
|
|
if (typeof (cmdArgs[1] - 0) === 'number' && !isNaN(cmdArgs[1] - 0)) { |
40
|
|
|
occ = (cmdArgs[1] - 0) |
41
|
|
|
if (occ < 1) { |
42
|
|
|
inpObj.error.push(errString + ' requires its second argument to be a numeric value of at least 1 being the instance required') |
43
|
|
|
return {found: false} |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
inpObj.error.push(errString + " requires its second argument to be numeric eg. '" + errString + " sometext 2' with the optional second argument being the instance required") |
47
|
|
|
return {found: false} |
48
|
|
|
} |
49
|
|
|
break |
50
|
|
|
default: |
51
|
|
|
inpObj.error.push(errString + " requires 1 or 2 arguments eg. '" + errString + " sometext' with the optional second argument being the instance required.") |
52
|
|
|
return {found: false} |
53
|
|
|
} |
54
|
|
|
let x = -1 |
55
|
|
|
let arg = removeQuotes(cmdArgs[0]) |
56
|
|
|
for (let i = 0; i < occ; i++) { |
57
|
|
|
x = inpObj.plain.indexOf(arg, x + 1) |
58
|
|
|
} |
59
|
|
|
if (x === -1) { |
60
|
|
|
inpObj.error.push('unable to find occurrence ' + occ + ' of "' + arg + '"') |
61
|
|
|
} |
62
|
|
|
return {found: (x !== -1), loc: x, len: arg.length} |
63
|
|
|
} |
64
|
|
|
|